package com.bsoft.gol.hbs.service.rpc.impl;

import com.bsoft.gol.hbs.dao.HiSysMsgTplDao;
import com.bsoft.gol.hbs.utils.BeanNotNullUtil;
import com.bsoft.gol.hbs.utils.CommonUtils;
import com.bsoft.gol.hbs.utils.ObjectId;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.message.HiSysMsgTplEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import pcn.bean.consultation.HiSysMsgTpl;
import pcn.bean.consultation.HiSysMsgTplList;
import com.bsoft.gol.hbs.service.rpc.TemplateConfigInfoIntf;

import javax.persistence.Transient;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:消息管理配置升级---模板配置
 * @author: zhengcb10708
 * @create: 2022-01-20 14:03
 **/
@SsdevService("templateConfigInfoService")
public class TemplateConfigInfoService implements TemplateConfigInfoIntf {
    private static Logger logger = LoggerFactory.getLogger(TemplateConfigInfoService.class);
    @Autowired
    private HiSysMsgTplDao hiSysMsgTplDao;

    /**
     * @Describe: 保存消息模板配置（支持新增、修改）
     * @Param: [hiSysMsgTplList]
     * @Return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: zhengcb10708
     * @Date: 2022/1/20 14:33
     */
    @Transient
    @RpcService
    @Override
    public Map<String, Object> saveTemplateConfigInfo(HiSysMsgTplList hiSysMsgTplList) throws ControllerException {
        logger.info("saveTemplateConfigInfo params"+hiSysMsgTplList);
        Map<String, Object> map= new HashMap<>();
        if(CommonUtils.isEmpty(hiSysMsgTplList.getTplConfigList())){
            throw new ControllerException("必要入参不能为空");
        }
        for(HiSysMsgTpl hiSysMsgTpl:hiSysMsgTplList.getTplConfigList()){
            BeanNotNullUtil.isNotEmptyBean(hiSysMsgTpl, Arrays.asList("id"));
        }
        //数据校验完毕后，开始入库
        for(HiSysMsgTpl hiSysMsgTpl:hiSysMsgTplList.getTplConfigList()){
            HiSysMsgTplEntity hiSysMsgTplEntity= new HiSysMsgTplEntity();
            BeanUtils.copyProperties(hiSysMsgTpl,hiSysMsgTplEntity);
            if(CommonUtils.isEmpty(hiSysMsgTpl.getId())){//新增
                //采用mongo 的 ObjectId 算法
                String objectId=new ObjectId().toHexString();
                hiSysMsgTplEntity.setId(objectId);
                hiSysMsgTplDao.save(hiSysMsgTplEntity);
            }else {
                hiSysMsgTplDao.update(hiSysMsgTplEntity);
            }
        }
        map.put("cd",hiSysMsgTplList.getTplConfigList().get(0).getCd());
        return map;
    }
    /**
     * @Describe: 删除消息模板配置
     * @Param: [hiSysMsgTpl]
     * @Return: void
     * @Author: zhengcb10708
     * @Date: 2022/1/20 14:33
     */
    @Transactional
    @RpcService
    @Override
    public void delTemplateConfigInfo(HiSysMsgTpl hiSysMsgTpl) throws ControllerException {
        logger.info("delTemplateConfigInfo id"+hiSysMsgTpl.getId());
        if(CommonUtils.isEmpty(hiSysMsgTpl.getId())){
            throw new ControllerException("记录id不能为空");
        }
        hiSysMsgTplDao.remove(hiSysMsgTpl.getId());
    }
    /**
     * @Describe:查询消息模板配置列表
     * @Param: [hiSysMsgTpl]
     * @Return: java.util.List<hcn.base.message.HiSysMsgTplEntity>
     * @Author: zhengcb10708
     * @Date: 2022/1/20 14:33
     */
    @RpcService
    @Override
    public List<HiSysMsgTplEntity> queryTemplateConfigInfoList(HiSysMsgTpl hiSysMsgTpl) throws ControllerException {
        logger.info("queryTemplateConfigInfoList cd"+hiSysMsgTpl.getCd());
        if(CommonUtils.isEmpty(hiSysMsgTpl.getCd())){
            throw new ControllerException("模板编码不能为空");
        }
        List<HiSysMsgTplEntity> hiSysMsgTplEntityList=hiSysMsgTplDao.findHiSysMsgTplList(hiSysMsgTpl.getCd());
        return hiSysMsgTplEntityList;
    }

}
